home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8448 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help with cin.getline()
  5. Date: 18 Feb 1996 17:17:37 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4g7mvh$p4i@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 16, 1996 21:42:44 in article <help with cin.getline()>,
  15. 'preddym@ucs.orst.edu (M Preddy)' wrote: 
  16.  
  17.  
  18. >Please forgive my newbie naivity, but I'm having trouble with  
  19. >cin.getline() under MSVC++ 4.0.  Why does the following snipit, 
  20. >    char cd_query[4];     
  21. >    do 
  22. >        { 
  23. >            cout << "Constant Cd (y/n)? "; 
  24. >            cin.getline(cd_query,4); 
  25. >        } while (cd_query[0] !='n' && cd_query[0] != 'y'); 
  26. >Produce the following output before stopping to wait for user input: 
  27. >    Constant Cd (y/n)? Constant Cd (y/n)? 
  28. >The do-loop executes once, without stopping for input until the second  
  29. >pass.  This is obviously not what I want.  Thank you for your help. 
  30.  
  31. Actually, it does.  Chances are that before the execution got 
  32. to this point of your code, you did something like "cin >> somedata;". 
  33. The pointer at that time was positioned at the newline character, 
  34. which the extraction operator put back ino the stream after 
  35. encountering it. 
  36.  
  37. When you call cin.getline(), it finds the newline, and returns an 
  38. empty line.  The next time around, it reads until the next newline 
  39. which is the one youentered after your y/n response.   
  40.  
  41. How to handle this situation is left as an exercise. 
  42. -- 
  43. Pete Grant 
  44. Kalevi, Inc. 
  45. Software Engineering & development
  46.